package com.huike.web.controller.clues;


import cn.hutool.core.bean.BeanUtil;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 线索跟进记录Controller
 *
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/clues/record")
public class TbClueTrackRecordController extends BaseController {
    @Autowired//依赖注入service层
    private ITbClueTrackRecordService tbClueTrackRecordService;

    /**
     * 查询线索跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("clueId") Long clueId) {
//        2.调用service回显,添加分页
        startPage();
       List<TbClueTrackRecord>list= tbClueTrackRecordService.list(clueId);
//       3.返回结果
        return getDataTable(list);
    }


    /**
     * 新增线索跟进记录
     */
    @PreAuthorize("@ss.hasPermi('clues:record:add')")
    @Log(title = "线索跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClueTrackRecordVo clueTrackRecordVo) {//接收请求参数
//        1.创建线索跟进对象
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
//        1.1为线索跟进对象复制属性
        BeanUtil.copyProperties(clueTrackRecordVo,tbClueTrackRecord);
//        1.2为线索跟进对象设置创建人和创建时间
        tbClueTrackRecord.setCreateTime(new Date());
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
//        2.创建线索对象
        TbClue tbClue = new TbClue();
//        2.1为线索对象复制属性
        BeanUtil.copyProperties(clueTrackRecordVo,tbClue);//属性拷贝
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());//线索状态
        tbClue.setId(clueTrackRecordVo.getClueId());//线索id

//        调用sevice
        tbClueTrackRecordService.add(tbClueTrackRecord,tbClue);
        return AjaxResult.success();
    }
}
